<template>
  <div class="text_list_box">
    <el-table
      ref="tableRef"
      :data="tableData"
      height="400"
      max-height="550"
      tooltip-effect="light"
      class="table_box"
      header-row-class-name="table_header_title"
    >
      <el-table-column width="50px" align="center">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-checkbox
              v-model="scope.row.isCheck"
              @change="chooseFile(scope.row)"
            ></el-checkbox>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="标题" width="200" align="'center'">
        <template #default="scope">
          <div class="line3">{{scope.row.title}}</div>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="'center'">
        <template #default="scope">
          <div class="line3">{{scope.row.content}}</div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, watch, computed } from "vue";
import { ElMessage } from 'element-plus'
import { getSuffixIcon } from '@/utils/utils'

// const props = defineProps(["baseList"]);

interface PropsType {
    baseList?: any[],
    selectBaseList?: any[],
    remainNum?: (string | number),
    multiple?: boolean,
    totalNum: number
  }
const props = withDefaults(defineProps<PropsType>(), {
  baseList: ()=>[],
  selectBaseList: ()=>[],
  remainNum: 0,
  multiple: true,
  totalNum: 9
})


const emit = defineEmits(["getData"]);
let textArr:any = [];

const chooseFile = (item: any) => {
    let indx = props.selectBaseList.findIndex((it: any) => it.id === item.id)
    if(props.multiple){
      if (indx === -1) {
        if (props.remainNum <= 0) {
          // ElMessage.warning(`最多可选${props.selectBaseList.length == 0 ? 9 : props.selectBaseList.length}个素材`)
          ElMessage.error(`最多可选${props.totalNum}个素材`);
          return
        }
        item.isCheck = true
        props.selectBaseList.push(item)
      } else {
        props.selectBaseList.splice(indx, 1)
        item.isCheck = false
      }
    }else{
      if (indx === -1) {
        props.baseList.forEach(ite=>{
          ite.isCheck = false
        })
        item.isCheck = true
        props.selectBaseList.splice(0)
        props.selectBaseList.push(item)
      } else {
        item.isCheck = false
        props.selectBaseList.splice(0)
      }
    }
    emit('getData', {
      type: 1,
      typeStr: 'text',
      data: props.selectBaseList
    })
}

const tableData = computed(() => {
  let arr: any[] = []
  props.baseList.forEach((item: any) => {
    item.isCheck = false
    arr.push(item)
  })
  return arr
});

</script>

<style lang="scss" scoped>
.text_list_box {
  width: 100%;
  .line3{
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    word-break: break-all;  
  }
}
</style>
<style lang="scss">
.text_list_box {
  .el-table--scrollable-y ::-webkit-scrollbar {
    display: none;
  }
  .table_header_title {
    th.el-table__cell {
      background: #f9f9f9;
      padding: 10px 0;
    }
  }
}
</style>
